#include "MainWidget.h"
#include "ui_MainWidget.h"
#include <QHostAddress>
#include <QMessageBox>
#include <QNetworkDatagram>
#include <QDebug>
MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    m_sendSocket = new QUdpSocket(this);
    m_recvSocket = new QUdpSocket(this);
    ui->btn_close->setEnabled(false);
    setWindowTitle("MulticastGroupTester");


    m_aboutDialog = new DialogAbout(this);

}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::on_btn_init_clicked()
{
    m_addr = QHostAddress(ui->ldt_addr->text());
    bool isOk1, isOk2;
    m_sendPort = ui->ldt_sendPort->text().toInt(&isOk1);
    m_recvPort = ui->ldt_recvPort->text().toInt(&isOk2);
    if(!isOk1 || !isOk2)
    {
        QMessageBox::warning(this,"Attention","PortError!",QMessageBox::Ok,QMessageBox::Ok);
        return ;
    }

    if(!m_addr.isMulticast())
    {
        QMessageBox::warning(this,"Attention","The IP is NOT a MuticastGroup Addr!",QMessageBox::Ok,QMessageBox::Ok);
        return ;
    }
    if(ui->checkBox_send->checkState() == Qt::Checked)
    {
        m_sendSocket->bind(QHostAddress::AnyIPv4, m_sendPort, QUdpSocket::ShareAddress);
        m_sendSocket->joinMulticastGroup(m_addr);
    }

    if(ui->checkBox_recv->checkState() == Qt::Checked)
    {
        m_recvSocket->bind(QHostAddress::AnyIPv4, m_recvPort, QUdpSocket::ShareAddress);
        m_recvSocket->joinMulticastGroup(m_addr);
        connect(m_recvSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    }

    ui->checkBox_recv->setEnabled(false);
    ui->checkBox_send->setEnabled(false);

    ui->btn_init->setEnabled(false);
    ui->btn_close->setEnabled(true);

    m_count = 0;
    ui->lbl_count->setText(QString::number(m_count));

    ui->lbl_state->setText("-WORKING-");
}

void MainWidget::on_btn_close_clicked()
{
    if(ui->checkBox_send->checkState() == Qt::Checked)
    {
        m_sendSocket->leaveMulticastGroup(m_addr);
        m_sendSocket->close();
    }
    if(ui->checkBox_recv->checkState() == Qt::Checked)
    {
        m_recvSocket->leaveMulticastGroup(m_addr);
        m_recvSocket->close();
        disconnect(m_recvSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    }


    ui->checkBox_recv->setEnabled(true);
    ui->checkBox_send->setEnabled(true);
    ui->btn_init->setEnabled(true);
    ui->btn_close->setEnabled(false);
    m_count = 0;
    ui->lbl_count->setText(QString::number(m_count));

    ui->lbl_state->setText("-WAITING FOR INIT-");
}

void MainWidget::on_btn_send_clicked()
{
    if(ui->btn_close->isEnabled())
        m_sendSocket->writeDatagram(QString("%1:abcdefghjklmnopqrstuvwxyz").arg(m_count%10).toLatin1(),m_addr,m_recvPort);
}

void MainWidget::onReadyRead()
{

    qDebug() << m_recvSocket->receiveDatagram().data();
    m_count++;
    ui->lbl_count->setText(QString::number(m_count));
}

void MainWidget::on_tbtn_about_clicked()
{
    m_aboutDialog->show();
}


void MainWidget::on_checkBox_send_clicked(bool checked)
{
    ui->btn_send->setEnabled(checked);
}
